"""
 Pandas数据分析实战
 第1部分 Pandas 核心基础
 第1章 Pandas 概述
"""

import pandas as pd


# 使用 read_csv 函数导入 movies.csv 文件
def read_csv():
    movies = pd.read_csv('./file/chapter_01/movies.csv')
    print(movies.head())
    # movies.tail()
    # len(movies)
    # movies.shape
    # movies.size

    # print(movies.iloc[499])
    # movies['Title']
    # print(movies.index)


def read_csv_index():
    movies = pd.read_csv("./file/chapter_01/movies.csv", index_col="Title")
    # print(movies.iloc[499])
    # print(movies.sort_values(by='Year', ascending=False).head())
    # print(movies.sort_values(by=['Studio','Year']).head())
    print(movies.sort_index().head())


def get_series():
    movies = pd.read_csv("./file/chapter_01/movies.csv", index_col="Title")
    print(movies['Studio'].value_counts().head(10))


def filter_dataframe():
    movies = pd.read_csv("./file/chapter_01/movies.csv", index_col="Title")
    # filter_by_universal = movies["Studio"] == "Universal"
    # filter_by_year = movies['Year'] == 2015
    # movies[filter_by_universal | filter_by_year]
    # movies[filter_by_universal & filter_by_year]

    # filter_by_1975 = movies['Year'] < 1975
    # movies[filter_by_1975]
    # print(movies[filter_by_1975])

    # filter_by_1983 = movies["Year"] >= 1983
    # filter_by_1986 = movies["Year"] <=1986
    # print(movies[filter_by_1983&filter_by_1986])

    # year__between = movies["Year"].between(1983, 1986)
    # print(movies[year__between])

    str_lower__str_contains = movies.index.str.lower().str.contains("dark")
    print(movies[str_lower__str_contains])


def group_by():
    movies = pd.read_csv("./file/chapter_01/movies.csv", index_col="Title")
    gross_series = movies['Gross'].str.replace("$", "", regex=False).str.replace(",", "", regex=False).astype(float)
    movies['Gross'] = gross_series
    studios = movies.groupby("Studio")
    print(studios['Gross'].sum().sort_values(ascending=False).head())
    # print(studios['Gross'].count().sort_values(ascending=False).head())
    # print(studios['Gross'].count().sort_values("Studio").head())
    # print(movies['Gross'].mean())
    # print(gross_series.dtype)
    # movies['Gross'] = gross_series
    # print(movies)


if __name__ == '__main__':
    group_by()
